package com.losdun.points.controller.admin;

import com.losdun.common.core.controller.BaseController;
import com.losdun.common.core.domain.AjaxResult;
import com.losdun.common.core.page.TableDataInfo;
import com.losdun.points.domain.PointsGoodsCategory;
import com.losdun.points.service.PointsGoodsCategoryService;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * @Description
 * @Author Sword.Ye
 * @Date 2025/7/8
 **/
@RestController
@Slf4j
@RequestMapping(value = "/points/category")
public class PointsGoodsCategoryController extends BaseController {
    @Autowired
    private PointsGoodsCategoryService pointsGoodsCategoryService;

    @PreAuthorize("@ss.hasPermi('points:category:query')")
    @GetMapping("/list")
    @ApiOperation("查询列表")
    public TableDataInfo list(PointsGoodsCategory pointsGoodsCategory) {
        startPage();
        List<PointsGoodsCategory> list= pointsGoodsCategoryService.queryList(pointsGoodsCategory);
        return getDataTable(list);
    }
    @PreAuthorize("@ss.hasPermi('points:category:query')")
    @GetMapping("/listAll")
    @ApiOperation("查询所有")
    public AjaxResult listAll() {
        List<PointsGoodsCategory> list= pointsGoodsCategoryService.queryList(new PointsGoodsCategory());
        return success(list);
    }


    @PreAuthorize("@ss.hasPermi('points:category:edit')")
    @PostMapping("/update")
    @ApiOperation("更新")
    public AjaxResult update(@RequestBody PointsGoodsCategory category){
        try {
            pointsGoodsCategoryService.updateById(category);
            return AjaxResult.success("更新成功");
        } catch (Exception e) {
            log.error(e.getMessage(),e);
            return AjaxResult.error("更新失败！");
        }
    }

    @PreAuthorize("@ss.hasPermi('points:category:remove')")
    @PostMapping("/delete/{id}")
    @ApiOperation("删除")
    public AjaxResult delete(@PathVariable("id") Long id){
        try {
            pointsGoodsCategoryService.removeById(id);
            return AjaxResult.success("删除成功");
        } catch (Exception e) {
            log.error(e.getMessage(),e);
            return AjaxResult.error("删除失败！");
        }
    }
    @PreAuthorize("@ss.hasPermi('points:category:add')")
    @PostMapping("/insert")
    @ApiOperation("新增")
    public AjaxResult insert(@RequestBody PointsGoodsCategory category){
        try {
            pointsGoodsCategoryService.save(category);
            return AjaxResult.success("成功");
        } catch (Exception e) {
            log.error(e.getMessage(),e);
            return AjaxResult.error("新增失败！");
        }
    }

    /**
     * 获取积分商品分类详细信息
     */
    @PreAuthorize("@ss.hasPermi('points:category:query')")
    @GetMapping(value = "/detail/{id}")
    public AjaxResult getDetail(@PathVariable("id") Long id)
    {
        return success(pointsGoodsCategoryService.getById(id));
    }
}
